package com.houtai.service.company;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdCompany;
import com.houtai.bean.dto.company.TdCompanyListDto;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdCompanyDao;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.UserDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyService {
    private static Logger logger = Logger.getLogger(CompanyService.class);
    @Autowired
    private TdCompanyDao tdCompanyDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;

    /**
     * @Purpose   查找黑名单
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findTdCompanyList(TdCompanyListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findTdCompanyList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "company_id desc");
            List<TdCompany> lists = tdCompanyDao.findCompanyList(page);
            PageInfo<TdCompany> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("company_name", page.getCompany_name());

          
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找企业" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   审核企业
     * @param     tdCompany
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo auditTdCompany(TdCompany tdCompany, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":auditTdCompany:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCompany detail = tdCompanyDao.findTdCompany(tdCompany.getCompany_id());
            if (detail != null && detail.getState() == 1) {
                String result;
                detail.setState(tdCompany.getState());

                if (detail.getState() == 2) {
                    detail.setAudit_remark("审核通过");
                    result = "审核通过";
                    //更新用户认证结果 昵称
                    userDao.updateUserNick(detail.getCompany_name(), detail.getUser_id());
                } else {
                    detail.setAudit_remark(tdCompany.getAudit_remark());
                    result = "审核不通过 原因：" + tdCompany.getAudit_remark();
                }

                tdCompanyDao.updateTdCompany(detail);
                retInfo.setMark("0");
                retInfo.setTip("成功.");
                LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"审核企业,id:" + tdCompany.getCompany_id()
                        + "结果：" + result,request);
            } else {
                retInfo.setMark("1");
                retInfo.setTip("不可审核.");
            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
   
}
